#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int breakfastNumber(vector<int>& staple, vector<int>& drinks, int x) {
        sort(staple.begin(), staple.end(), greater<int>());
        sort(drinks.begin(), drinks.end());
        int MOD = (int)1e9 + 7;

        int ret = 0;
        int sn = staple.size();
        int pos = 0;
        int fi = 0;
        int count = 0;
        for (int i = 0; i < sn; ++i)
        {
            fi = x - staple[i];
            if (fi < 1)
                continue;

            count = upper_bound(drinks.begin() + pos, drinks.end(), fi) - (drinks.begin() + pos);

            ret += count * (sn - i);
            pos += count;
            ret %= MOD;
        }

        return ret;
    }
};